package ouisseuk.gui;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import ouisseuk.model.Artist;
import ouisseuk.model.Image;
import ouisseuk.model.SearchResult;
import ouisseuk.tools.Discogs;

import com.flexycore.iphone.coregraphics.CGRect;
import com.flexycore.iphone.coregraphics.CGSize;
import com.flexycore.iphone.uikit.UIColor;
import com.flexycore.iphone.uikit.UILabel;
import com.flexycore.iphone.uikit.UINavigationController;
import com.flexycore.iphone.uikit.UIScrollView;
import com.flexycore.iphone.uikit.UITextAlignment;
import com.flexycore.iphone.uikit.UIWebView;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
public class ArtistView extends UIScrollView {

	public ArtistView(SearchResult searchResult, final UINavigationController n) {
		super();
		init();
		setBackgroundColor(UIColor.whiteColor());

		Artist artist = null;
		try {
			artist = Discogs.getArtist(searchResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int totalY = 0;

		/*
		 * Name
		 */

		UILabel matureProperty = new UILabel();
		matureProperty.initWithFrame(CGRect.CGRectMake(0, 10, 320, 30));
		matureProperty.setBackgroundColor(UIColor.clearColor());
		matureProperty.setTextColor(UIColor.blackColor());

		String artistName = artist.getName();
		matureProperty.setText(artistName);
		matureProperty.setTextAlignment(UITextAlignment.UITextAlignmentCenter);
		addSubview(matureProperty);

		totalY += 40;

		if (!artist.getImages().isEmpty()) {
			Image image = artist.getImages().get(0);
			String smallUri = image.getSmallUri();
			CGRect webFrame = new CGRect();
			webFrame.init(85, totalY + 20, 150, 150);

			totalY += 160;

			UIWebView aWebView = new UIWebView();
			aWebView.initWithFrame(webFrame);
			aWebView.setBackgroundColor(UIColor.blackColor());
			aWebView.setScalesPageToFit(true);

			String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
					+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
					+ "<head>"
					+ "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />"
					+ "<meta name=\"viewport\" content=\"width = 150\">"
					+ "</head>"
					+ "<body>"
					+ "<img src="
					+ smallUri
					+ " />"
					+ "</body>" + "</html>";
			aWebView.loadHTMLStringBaseURL(html, "http://www.discogs.com/");

			// aWebView.setAutoresizesSubviews(true);

			addSubview(aWebView);
		}

		totalY += 10;

		int cellSize = 44;
		int tableSize = cellSize * artist.getReleases().size();

		ReleasesFromArtistTableView releasesTableView = new ReleasesFromArtistTableView();
		releasesTableView.initWithFrame(CGRect.CGRectMake(10, totalY, 300, totalY + tableSize));

		releasesTableView.setScrollEnabled(false);

		ReleasesFromArtistDataSource dataSource = new ReleasesFromArtistDataSource(artist);
		ReleasesFromArtistDelegate delegate = new ReleasesFromArtistDelegate(artist, n);
		releasesTableView.setDataSource(dataSource);
		releasesTableView.setUITableViewDelegate(delegate);

		addSubview(releasesTableView);

		totalY += tableSize;

		totalY += 10;

		System.out.println("totalY: " + totalY);

		this.setContentSize(new CGSize().init(320, totalY));
		this.setScrollEnabled(true);

	}
}
